import { getFileStream } from "@/apis/file";
import docIcon from "@/assets/fileIcons/doc.svg";
import excelIcon from "@/assets/fileIcons/excel.svg";
import folderIcon from "@/assets/fileIcons/folder.svg";
import gcfxIcon from "@/assets/fileIcons/gcfx.png";
import imgIcon from "@/assets/fileIcons/img.svg";
import pdfIcon from "@/assets/fileIcons/pdf.svg";
import rarIcon from "@/assets/fileIcons/rar.svg";
import txtIcon from "@/assets/fileIcons/txt.svg";
import { FileTwoTone } from "@ant-design/icons";
import { UploadFile } from "antd/es/upload";
import { BROWSER_PREVIEW_FILE_SUFFIX, downloadFile as downloadStreamFile, getCanFilePreview, getFileSuffix, openMessage } from "hy_dev_lib";
import { ReactSVG } from "react-svg";

// 文件对象 添加字段和后端统一
export interface IFileItem extends UploadFile {
  tid: any;
  mc: string; // 文件名称
  dz: string; // 文件地址
  dx: number; // 文件大小
  tjsj?: string; // 上传时间
  gs: string; // 文件后缀名

  zt: 0 | 1 | 2 | 3; // 不审核文件|待审核|退回|通过
  sfyxwj: 0 | 1; // 草稿|有效数据
  sfgcwj: 0 | 1; // 是否需要解析的工程文件
  fjfl?: number; // 附件分类
  ylwjlx: string; //预览文件地址
  wjflmc?: string; //文件分类名称
  jdid?: string; // 文件所属流程的jdid
  treeTitle?: string; // 工程预览 左边树显示的文件名
  ywlzid?: string; // 业务流转id
}

// 根据文件地址将文件转换为Upload组建的组件的文件的文件对象
export const getFileObj = (file_path: string, file: any = {}, index: number = -1) => {
  if (!file_path) return null;
  return {
    uid: file.uid || index + "",
    name: file.name || file.file_name || file.mc || "",
    // size: file.size,
    ...file,
    status: "done",
    url: SYSTEM_GLOBAL.fileBaseURL + file_path,
  };
};

// 只有一个文件的直接返回一个数组
export const getSingleFileArr = (file_path: string, file?: any) => {
  if (!file_path) return [];
  return [getFileObj(file_path, file)];
};

// 返回能用于Upload组件的FileList
export const getUploadFileArr = (fileList: any[], urlFieldName: string = "dz") => {
  if (!fileList?.length) return [];
  return fileList.map((item: any, index: number) => getFileObj(item[urlFieldName], item, index));
};

// 获取文件地址 去掉上面拼接的 SYSTEM_GLOBAL.baseURL
export const getFilePath = (path: string) => {
  if (!path) return "";
  return path.replace?.(SYSTEM_GLOBAL.fileBaseURL, "");
};

// 文件图标
export const FileIcon = (props: any) => {
  const { fileName, ...rest } = props;
  const suffix = getFileSuffix(undefined, fileName);
  let icon;
  switch (suffix) {
    case ".doc":
    case ".docx":
      icon = docIcon;
      break;
    case ".xls":
    case ".xlsx":
      icon = excelIcon;
      break;
    case ".gcfx":
      icon = gcfxIcon;
      break;
    case ".png":
    case ".jpg":
    case ".gif":
      icon = imgIcon;
      break;
    case ".pdf":
      icon = pdfIcon;
      break;
    case ".zip":
    case ".rar":
      icon = rarIcon;
      break;
    case ".txt":
      icon = txtIcon;
      break;
    default:
      icon = folderIcon;
      break;
  }
  if (!icon) return <FileTwoTone {...rest} />;
  if (suffix === ".gcfx" || rest.isMap)
    return <img src={icon} style={{ lineHeight: 1, width: "1em", height: "1em" }} {...rest}></img>;
  return <ReactSVG className="react-svg" src={icon} style={{ lineHeight: 1, fontSize: 16 }} {...rest}></ReactSVG>;
};

/** 下载文件 */
export const downloadFile = async (file: any, name?: string) => {
  if (!file?.fjlj)
    return openMessage({
      type: "error",
      content: "没有可下载的文件！.",
    });

  const res = await getFileStream(file?.fjlj);
  downloadStreamFile(res, name || file.fjmc)
};

// 预览文件 通过 getGenerateViewerPath 预览的文件只需要tid
export const onPreviewFile = async (fileItem: IFileItem) => {
  const { tid, mc, dz, fjfl, ylwjlx } = fileItem;
  const suffix = getFileSuffix(undefined, mc || dz);
  // 不能预览的文件 直接下载
  if (!getCanFilePreview(mc || dz)) return downloadFile(fileItem, mc);
  if (BROWSER_PREVIEW_FILE_SUFFIX.includes(suffix) && dz)
    // return window.open("/down" + dz)
    return window.open(SYSTEM_GLOBAL.fileBaseURL + dz);
  // if (!ylwjlx) {
  //   //生成获取文件预览地址
  //   const data = await getGenerateViewerPath(dz, tid);
  //   //保存预览地址
  //   // await saveViewerUrl(tid, data);
  //   window.open(SYSTEM_GLOBAL.fileBaseURL + data, "_blank");
  // } else {
  //   window.open(SYSTEM_GLOBAL.fileBaseURL + ylwjlx, "_blank");
  // }
};


